<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('用户新增')"/>
</head>
<body>
<form class="layui-form">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">登录账号</label>
                    <div class="layui-input-block">
                        <input type="text" name="username" lay-verify="username" autocomplete="off" placeholder="登录账号" class="layui-input">
                    </div>

                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">用户密码</label>
                    <div class="layui-input-block">
                        <input type="password" name="password" lay-verify="password" autocomplete="off" placeholder="请输入密码" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-block">
                        <input type="text" name="nickname" lay-verify="nickname" autocomplete="off" placeholder="请输入人员姓名" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">出生年月</label>
                    <div class="layui-input-block">
                        <input type="text" name="userBirth" lay-verify="userBirth" autocomplete="off"  id="userBirth" class="layui-input datetime-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">性别</label>
                    <div class="layui-input-block">
                        <input  type="radio" name="manSex" value="0" title="男">
                        <input  type="radio" name="manSex" value="1" title="女">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">民族</label>
                    <div class="layui-input-block">
                        <input type="text" name="userNation" lay-verify="userNation" autocomplete="off" placeholder="请输入民族" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">职务</label>
                    <div class="layui-input-block">
                        <input type="text" name="userOffice" lay-verify="userOffice" autocomplete="off" placeholder="请输入职务" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">专业职称</label>
                    <div class="layui-input-block">
                        <input type="text" name="userTitle" lay-verify="userTitle" autocomplete="off" placeholder="请输入职称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">研究专长</label>
                    <div class="layui-input-block">
                        <input type="text" name="userExpertise" lay-verify="userExpertise" autocomplete="off" placeholder="请输入研究专长" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">电话</label>
                    <div class="layui-input-block">
                        <input type="text" name="userPhone" lay-verify="userPhone" autocomplete="off" placeholder="请输入电话" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label">部门</label>
                    <div class="layui-input-block">
                        <ul id="selectParent" name="deptId" class="dtree" data-id="0"></ul>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">通讯地址</label>
                    <div class="layui-input-block">
                        <input type="text" name="userAddress" lay-verify="userPhone" autocomplete="off" placeholder="请输入通讯地址" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">单位名称</label>
                    <div class="layui-input-block">
                        <input  readonly th:value="${deptName}" type="text" name="deptName" lay-verify="deptName" autocomplete="off" placeholder="请输入单位名称" class="layui-input">

                    </div>
                </div>

<!--                <div class="layui-form-item">-->
<!--                    <label class="layui-form-label">单位</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <select id="positionId" name="posId">-->
<!--                            <option th:each="list : ${sysPositionsList}" th:text="${list.postName}" th:value="${list.id}"></option>-->
<!--                        </select>-->
<!--                    </div>-->
<!--                </div>-->

                <div class="layui-form-item">
                    <label class="layui-form-label">邮政编码</label>
                    <div class="layui-input-block">
                        <input type="text" name="userZip" lay-verify="userZip" autocomplete="off" placeholder="请输入邮政编码" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">角色</label>
                    <div class="layui-input-block">
                        <input th:each="sysRole:${tsysRoleList}" th:value="${sysRole.id}" th:title="${sysRole.name}"
                               type="checkbox"  lay-skin="primary">
                    </div>
                </div>

            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>






<th:block th:include="include :: footer"/>
<script>
    layui.use(['form', 'jquery','dtree','laydate'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let dtree = layui.dtree;
        let MODULE_PATH = rootPath+"/UserDanweiController";
        let DEPAR_PATH=rootPath+"/SysDepartmentController";
        let laydate = layui.laydate;

        //绑定事件选择器
        laydate.render({
            elem: '#userBirth' //指定元素,
            ,type: 'date'
            ,format: 'yyyy-MM-dd HH:mm:ss'
        });




        //名称验证
        form.verify({
            username: function(value){
                if(value.length < 2){
                    return '标题至少得2个字符啊';
                }
                var msg = "";
                $.ajax({
                    url: MODULE_PATH+'/checkLoginNameUnique',
                    data: "username="+value,
                    dataType: 'json',
                    type: 'post',
                    async:false,
                    success: function (result) {
                            msg=result;
                    }
                });
                if (msg == 1) {
                   return "已有相同数据";
                }

            }
        });



        //保存
        form.on('submit(user-save)', function (data) {

            let roleIds = "";
            $('input[type=checkbox]:checked').each(function () {
                roleIds += $(this).val() + ",";
            });
            roleIds = roleIds.substr(0, roleIds.length - 1);
            data.field.roleIds = roleIds;

            if($("#deptNameSelect option:selected").val() == "其他" || $("#deptNameSelect option:selected").val() == "社科类社会组织"){
                data.field.deptName = $("#other").val();
            }
            $.ajax({
                url: MODULE_PATH+'/add',
                data: data.field,
                dataType: 'json',
                contentType: 'application/x-www-form-urlencoded',
                type: 'post',
                success: function (result) {
                    if (result.code==200) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("user-table");
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });
    })
</script>
</body>
</html>